﻿#ifndef LOGGER_H
#define LOGGER_H
#pragma once

#include <string>
#include <fstream>
#include <thread>
#include "MutexQueue.h"
// 异步写文件
// 饿汉模式单例类
#define LOG_INS Logger::getInstance()
class Logger
{
public:
	enum Type { CONSOLE, FILE };
	enum Level { DEBUG, INFO, WARNING, ERROR, CRITICAL };
	static Logger* getInstance();
	void Log(const std::string& , const std::string& file, const std::string& function,int line, Level level = INFO);
	void setEnableLevel(Level level);
	inline void setDevice(Type device)
	{
		m_device = device;
	}
	~Logger();
private:
	Logger();
	Logger(const Logger& log) = delete;
	Logger(Logger&&) = delete;
private:
	MutexQueue<std::string> que_;
	std::ofstream m_writer;
	static Logger m_log;
	Level m_level = INFO;
	Type m_device = CONSOLE;

	void workerTask();
};
#define LOG_INFO(text, INFO, TYPE) do { \
    LOG_INS->setDevice(TYPE); \
    LOG_INS->setEnableLevel(INFO); \
    LOG_INS->Log(text, __FILE__, __FUNCTION__, __LINE__, INFO); \
} while (0)
//inline void LOG_INFO(const std::string& text, Logger::Level level = Logger::INFO, Logger::Type device = Logger::FILE) {
//    LOG_INS->setDevice(device);
//    LOG_INS->setEnableLevel(level);
//    Logger::getInstance()->Log(text, __FILE__, __FUNCTION__ ,__LINE__, Logger::INFO);
//}
inline void LOG_ERROR(const std::string&, Logger::Level level = Logger::INFO, Logger::Type device = Logger::FILE);

#endif // LOGGER_H